package com.nuoniu.sibanyun.controller.work;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.work.WorkReportForm;
import com.nuoniu.sibanyun.entity.work.WorkReportUser;
import com.nuoniu.sibanyun.service.work.IWorkReportFormService;
import com.nuoniu.sibanyun.service.work.IWorkReportUserService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 工作报表
 * @auther 小啊刚
 * @date 2020-08-14
 */
@RestController
@RequestMapping("work/reportForm")
@Api(tags = "工作报表", value = "工作报表")
public class WorkReportFormController {

    @Autowired
    private IWorkReportFormService reportFormService;

    /**
     * 获取工作报表列表
     * @author 小啊刚
     * @date 2020-08-14
     */
    @GetMapping("pageList")
    public HttpResult pageList(Page page, WorkReportForm reportForm) {
        return HttpResult.ok(reportFormService.pageList(page, reportForm));
    }

    /**
     * 保存工作报表
     * @author 小啊刚
     * @date 2020-08-15
     */
    @PostMapping("save")
    public HttpResult saveReportForm(@RequestBody WorkReportForm reportForm) throws ParseException {
        reportFormService.saveReportForm(reportForm);
        return HttpResult.ok();
    }

    /**
     * 撤回
     * @author 小啊刚
     * @date 2020-08-26
     */
    @PostMapping("returnReportForm")
    public HttpResult returnReportForm(@RequestBody WorkReportForm reportForm) {
        reportFormService.returnReportForm(reportForm);
        return HttpResult.ok();
    }

    /**
     * 删除工作报表
     * @author 小啊刚
     * @date 2020-08-15
     */
    @PostMapping("delete")
    public HttpResult delete(@RequestBody List<String> ids) {
        for(String id : ids) {
            WorkReportForm reportForm = reportFormService.getById(id);
            if(reportForm.getStatus() != 0) {
                throw new NuoNiuException("已报送，不能删除");
            }
            reportFormService.removeById(id);
        }
        return HttpResult.ok();
    }

    /**
     * 报送报表
     * @author 小啊刚
     * @date 2020-08-17
     */
    @PostMapping("submitReportForm")
    public HttpResult submitReportForm(@RequestBody WorkReportForm reportForm) throws ParseException {
        if(reportForm.getSubmitId() == null) {
            throw new NuoNiuException("报送人不能为空");
        }

        if(reportForm.getStatus() != 0) {
            throw new NuoNiuException("请选择未报送的报表");
        }

        reportFormService.submitReportForm(reportForm);
        return HttpResult.ok();
    }

    /**
     * 评阅报表
     * @author 小啊刚
     * @date 2020-08-17
     */
    @PostMapping("readReportForm")
    public HttpResult readReportForm(@RequestBody WorkReportForm reportForm) {
        UserInfo userInfo = UserThreadLocal.get();
        WorkReportForm form = reportFormService.getById(reportForm.getId());
        if(!userInfo.getUserId().equals(form.getSubmitId())) {
            throw new NuoNiuException("被报送人才能评价");
        }
        if(StringUtils.isNotBlank(form.getEvaluationLevel()) && StringUtils.isNotBlank(form.getEvaluationContent())) {
            throw new NuoNiuException("已评价,不能再次评价");
        }

        //reportForm.setStatus(1);
        reportForm.setReadStatus(1);
        reportFormService.updateById(reportForm);
        return HttpResult.ok();
    }

//    public static void main(String[] args) throws ParseException {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date d  = sdf.parse("2020-13-01");
//        System.out.println(d);
//
//        String format = String.format("%04d", 580);
//        System.out.println(format);
//
//        String format1 = String.format("%04d", 58885);
//        System.out.println(format1);
//        String str=null;
//
//        str=String.format("Hi,%s", "飞龙");          // 格式化字符串
//
//        System.out.println(str);                             // 输出字符串变量str的内容
//
//        System.out.printf("字母a的大写是：%c %n", 'A');
//
//        System.out.printf("3>7的结果是：%b %n", 3>7);
//
//        System.out.printf("100的一半是：%d %n", 100/2);
//
//        System.out.printf("100的16进制数是：%x %n", 100);
//
//        System.out.printf("100的8进制数是：%o %n", 100);
//
//        System.out.printf("50元的书打8.5折扣是：%f 元%n", 50*0.85);
//
//        System.out.printf("上面价格的16进制数是：%a %n", 50*0.85);
//
//        System.out.printf("上面价格的指数表示：%e %n", 50*0.85);
//
//        System.out.printf("上面价格的指数和浮点数结果的长度较短的是：%g %n", 50*0.85);
//
//        System.out.printf("上面的折扣是%05d%% %n", 85);
//
//        System.out.printf("字母A的散列码是：%h %n", 'A');
//    }
}
